const tryCatch = require('../../utils/tryCatch');
const path = require("path")
const fs = require("fs")
const { uploadFile } = require("../../utils/qiniuupload")
const { getPublicKey } = require('../../utils/crypto');
const commonController = {
    // 上传文件
    uploadController: tryCatch((req, res) => {
        const filepath = path.resolve(process.cwd(), 'uploads', req.file.originalname)
        uploadFile(req.file.originalname, "img/", (fileres) => {
            fs.unlink(filepath, (err) => {
                if (err) throw err;
                console.log(`${req.file.originalname} was deleted`);
            })
            res.send({
                statusCode: 200,
                data: {
                    url: fileres.data.key,
                    name: fileres.data.name,
                }
            })
        }, (fileres) => {
        })
    }),
    // 发送公钥接口
    sendPublicKey: tryCatch((req, res) => {
        const publicKey = getPublicKey()
        res.send({
            statusCode: 200,
            data: {
                publicKey
            }
        })
    })

}

module.exports = commonController;// 引入User模型